﻿/****************************** Module Header ******************************\
 * Module Name:  ADFS.cs
 * Module Version:  1.0.0 (2012-10-3)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to application data file system.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System.IO;
using System.Collections.Generic;

namespace UCF.Core.ADF
{
    /// <summary>
    /// 应用程序数据文件
    /// </summary>
    public class ADFS
    {
        /// <summary>
        /// 获取或设置应用程序根路径
        /// </summary>
        public static string Root { get; set; }

        // 私有实例
        private static ADFS instance;

        /// <summary>
        /// 获取或设置ADF实例
        /// </summary>
        public static ADFS Current
        {
            get
            {
                if (instance == null) instance = new ADFS();
                return instance;
            }
        }

        /// <summary>
        /// 获取或设置Site目录
        /// </summary>
        public ADFS Site { get; set; }

        /// <summary>
        /// 获取或设置System目录
        /// </summary>
        public ADFS System { get; set; }

        /// <summary>
        /// 获取或设置Log目录
        /// </summary>
        public ADFS Log { get; set; }

        /// <summary>
        /// 获取或设置Backup目录
        /// </summary>
        public ADFS Backup { get; set; }

        /// <summary>
        /// 实例化ADF对象
        /// </summary>
        public ADFS()
        {
            this.Site = Directory.Exists(Root + "\\Site") ? new DirectoryInfo(Root + "\\Site") : Directory.CreateDirectory(Root + "\\Site");
            this.System = Directory.Exists(Root + "\\System") ? new DirectoryInfo(Root + "\\System") : Directory.CreateDirectory(Root + "\\System");
            this.Log = Directory.Exists(Root + "\\Log") ? new DirectoryInfo(Root + "\\Log") : Directory.CreateDirectory(Root + "\\Log");
            this.Backup = Directory.Exists(Root + "\\Backup") ? new DirectoryInfo(Root + "\\Backup") : Directory.CreateDirectory(Root + "\\Backup");
        }

        /// <summary>
        /// 压缩整个ADF文件夹
        /// </summary>
        public void Zip()
        {
            new FileHelper().ZipFolder(Root, Root + "\\Log\\ADF.zip", new string[] { Root + "\\Log" });
        }

        /// <summary>
        /// 解压缩还原ADF文件夹
        /// </summary>
        public void UnZip() {
            new FileHelper().UnZip(Root+"\\Log\\ADF.zip", Root);
        }

        // 文件夹对象缓存
        private DirectoryInfo cache;

        /// <summary>
        /// 实例化文件夹为ADF对象
        /// </summary>
        /// <param name="di"></param>
        /// <returns></returns>
        public static implicit operator ADFS(DirectoryInfo di)
        {
            return new ADFS(di);
        }

        /// <summary>
        /// 通过文件夹对象实例化ADF对象
        /// </summary>
        /// <param name="di"></param>
        public ADFS(DirectoryInfo di)
        {
            this.cache = di;
        }

        // 子文件夹集合
        private Dictionary<string, ADFS> folders;

        /// <summary>
        /// 获取指定名称的文件夹对象
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public ADFS GetFolder(string name)
        {
            if (this.folders == null)
            {
                this.folders = new Dictionary<string, ADFS>();
                foreach (DirectoryInfo di in cache.GetDirectories())
                {
                    this.folders.Add(di.Name, di);
                }
            }
            if (this.folders.ContainsKey(name)) return this.folders[name];
            return this.cache.CreateSubdirectory(name);
        }

        /// <summary>
        /// 获取指定名称的文件对象
        /// </summary>
        /// <param name="name"></param>
        /// <param name="ext"></param>
        /// <returns></returns>
        public string GetFileText(string name, string ext = ".txt")
        {
            name = "{0}{1}".Fmt(name, ext);
            if (!File.Exists(this.cache.FullName + "\\" + name)) File.WriteAllText(this.cache.FullName + "\\" + name, string.Empty);
            return File.ReadAllText(this.cache.FullName + "\\" + name);
        }

        /// <summary>
        /// 写入指定名称的文件对象
        /// </summary>
        /// <param name="name"></param>
        /// <param name="v"></param>
        /// <param name="ext"></param>
        public void SetFileText(string name,string v,string ext=".txt")
        {
            name = "{0}{1}".Fmt(name, ext);
            if (!File.Exists(this.cache.FullName + "\\" + name)) File.WriteAllText(this.cache.FullName + "\\" + name, string.Empty);
            File.WriteAllText(this.cache.FullName + "\\" + name, v);
        }

        /// <summary>
        /// 删除指定名称的文件对象
        /// </summary>
        /// <param name="name"></param>
        /// <param name="ext"></param>
        public void RemoveFile(string name, string ext = ".txt")
        {
            File.Delete(this.cache.FullName + "\\" + name);
        }

        /// <summary>
        /// 判断指定名称的文件对象是否存在
        /// </summary>
        /// <param name="name"></param>
        /// <param name="ext"></param>
        /// <returns></returns>
        public bool ExistsFile(string name, string ext = ".txt")
        {
            return File.Exists(this.cache.FullName + "\\" + name);
        }

        /// <summary>
        /// 返回当前目录的地址
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return cache.FullName;
        }
    }
}
